/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedbeans;

import entities.CompteBancaire;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.inject.Named;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.SortOrder;
import session.GestionnaireDeCompteBancaire;

/**
 *  gestion des sessions voir cour slide 103
 * @author Cyn
 */
@Named(value = "compteMBean")  
@SessionScoped
public class CompteMBean implements Serializable {
    @EJB
    private GestionnaireDeCompteBancaire gc;
    private LazyDataModel<CompteBancaire> modele;
    private CompteBancaire selectC;
    private List<CompteBancaire> datasource = new ArrayList();

    public CompteMBean() {
        modele = new LazyDataModel<CompteBancaire>(){

            @Override
            public List<CompteBancaire> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,String> filters) {
                System.out.println("load first :"+first+" et taille de page : "+pageSize+" sortfield : "+sortField+" Sort order : "+sortOrder.name()+" "+filters.toString());
                datasource = gc.getLazyComptes(first, pageSize, sortField, sortOrder.name());
                return datasource;
            }            

            @Override     
            public int getRowCount() {       
                return gc.getNBComptes();      
            }
            
            @Override  
            public Object getRowKey(CompteBancaire compte) {  
                return compte.getId();  
            }  
            
            @Override  
            public CompteBancaire getRowData(String rowKey) {  
                for(CompteBancaire compte : datasource) {  
                    if(compte.getId() == Long.parseLong(rowKey))  
                        return compte;  
                }  

                return null;  
            }  
        };     
    }
    
    public void setModele(LazyDataModel modele) {
        this.modele = modele;
    }

    public LazyDataModel getModele() {
        return modele;
    }

    public CompteBancaire getSelectC() {
        return selectC;
    }

    public void setSelectC(CompteBancaire selectC) {
        this.selectC = selectC;
    }    
    
    public void onRowSelect(SelectEvent event) {  
        
        //this.selectC = ((CompteBancaire) event.);
        //System.out.println("Compte Selectionné ! c : "+this.selectC.getId());
        
        FacesMessage msg = new FacesMessage("Compte Selected", ((CompteBancaire) event.getObject()).getType().getLibelleType());  
  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }
    
    public void onRowUnselect(UnselectEvent event) {  
        FacesMessage msg = new FacesMessage("Compte Unselected", ((CompteBancaire) event.getObject()).getType().getLibelleType());  
  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
    }
    
}
